package cn.edu.neusoft.food229.model;

import cn.edu.neusoft.food229.bean.IsCollectBean;
import cn.edu.neusoft.food229.bean.Result;
import cn.edu.neusoft.food229.iface.Collectiface;
import cn.edu.neusoft.food229.listener.CollectListener;
import cn.edu.neusoft.food229.listener.isCollectListener;
import cn.edu.neusoft.food229.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class CollectModel implements Collectiface {
    private Retrofit retrofit;
    public CollectModel(){
        //使用Retrofit--1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void collect(final String user_id, final String shop_food_id, final int flag, final isCollectListener listener) {
        CollectService  collectService=retrofit.create(CollectService.class);
        Call<IsCollectBean> call=collectService.collect(user_id,shop_food_id,flag);
        call.enqueue(new Callback<IsCollectBean>() {
            @Override
            public void onResponse(Call<IsCollectBean> call, Response<IsCollectBean> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponce(response.body(),user_id,shop_food_id,flag);
                else
                    listener.onFail("判断失败");
            }

            @Override
            public void onFailure(Call<IsCollectBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void collectFood(final String user_id, final String food_id, final CollectListener listener) {
        CollectService  collectService=retrofit.create(CollectService.class);
        Call<Result> call=collectService.collectFood(user_id,food_id);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponce(response.body(),user_id,food_id);
                else
                    listener.onFail("fail!!!!");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                listener.onFail("收藏失败："+t.toString());
            }
        });
    }

    @Override
    public void collectShop(final String user_id, final String shop_id, final CollectListener listener) {
        CollectService  collectService=retrofit.create(CollectService.class);
        Call<Result> call=collectService.collectShop(user_id,shop_id);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                if(response.isSuccessful() && response.body()!=null)
                    listener.onResponce(response.body(),user_id,shop_id);
                else
                    listener.onFail("shibai");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                listener.onFail("收藏失败："+t.toString());
            }
        });
    }
}
